package com.itheima.dao.impl;

import com.itheima.dao.AccountDao;
import com.itheima.pojo.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;


public class AccountDaoImpl implements AccountDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void transferIn(int inId, double money) {
        String sql = "update account set money=money - ? where id = ?";
        jdbcTemplate.update(sql, money, inId);
    }

    @Override
    public void transferOut(int outId, double money) {
        String sql = "update account set money=money + ? where id = ?";
        jdbcTemplate.update(sql, money, outId);
    }

    @Override
    public Account findAccountByID(Integer id) {
        String sql = "select * from account where id = ?";
        Account account = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class), id);
        return account;
    }


}
